package com.HospitalOutpatient.service.impl;

import com.HospitalOutpatient.entity.User;
import com.HospitalOutpatient.service.TokenService;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service("tokenService")
public class TokenServiceImpl implements TokenService {
    @Override
    public String getToken(User user) {
        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        return JWT.create()
                // 将 userId 保存在 token 中
                .withAudience(user.getUserId())
                // 设置过期时间
                .withExpiresAt(date)
                // 将 password 设置为 token 的密钥
                .sign(Algorithm.HMAC256(user.getPassWord()));

    }
}
